﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class QcOptionFrm : BaseFrm
    {
        //调用：
        //
        //质量问题
        //QcOptionFrm frm = new QcOptionFrm();        
        //frm.SetTableAsPromble();
        //frm.Show();
        //
        //质量结果
        //QcOptionFrm frm = new QcOptionFrm();        
        //frm.SetTableAsResult();        
        //frm.Show();

        /// <summary>
        /// 选择器窗体返回项
        /// </summary>
        public SelectItem SelectorItem { get; set; }

        /// <summary>
        /// 记住当前设置的编码规则
        /// </summary>
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        public enum EditTableType { enPromble, enResult };
        public EditTableType TableType { get; set; }
        private UserCtrl.UDataGridView EditView;
        private bool _SearchMode;

        /// <summary>
        /// 需要选择记录返回调用的情况下，进入窗体前调用此方法，进入窗体编辑记录不需使用。
        /// </summary>
        public void SearchMode()
        {
            _SearchMode = true;
        }

        public QcOptionFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void QcOptionFrm_Load(object sender, EventArgs e)
        {
            switch (TableType)
            {
                case EditTableType.enPromble:
                    //tB_QCResultBindingSource.DataSource = null;
                    gvQCResult.Visible = false;
                    gvQCProblem.Dock = DockStyle.Fill;
                    this.Text = "质量问题";
                    EditView = gvQCProblem;
                    EditView.AutoGenerateColumns = false;
                    EditView.DataSource = BaseDataProxy.FindQCProblemTypes();
                    break;
                case EditTableType.enResult:
                    //tB_QCProblemTypeBindingSource.DataSource = null;
                    gvQCProblem.Visible = false;
                    gvQCResult.Dock = DockStyle.Fill;
                    this.Text = "质量结果";
                    EditView = gvQCResult;
                    EditView.AutoGenerateColumns = false;
                    EditView.DataSource = BaseDataProxy.FindQCResults();
                    break;
            }
            EditView.CellDoubleClick += new DataGridViewCellEventHandler(EditView_CellDoubleClick);
            EditView.CellFormatting += new DataGridViewCellFormattingEventHandler(gvDetail_CellFormatting);
            btnSelect.Visible = _SearchMode;
        }

        void gvDetail_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            Common.CellFormatColor(sender, e, Common.BillOperateType.View);
        }

        void EditView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (_SearchMode)
                GetResult();
            else
                EdtSelect();
        }

        public void SetTableAsPromble()
        {
            TableType = EditTableType.enPromble;
            Name = EditTableType.enPromble.ToString();
        }

        public void SetTableAsResult()
        {
            TableType = EditTableType.enResult;
            Name = EditTableType.enResult.ToString();
        }

        private void RefreshData()
        {
            //必须关闭连接才可以从后台刷新数据
            this.BaseDataProxy.Close();
            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
            if (TableType == EditTableType.enPromble)
                EditView.DataSource = BaseDataProxy.FindQCProblemTypes();
            else
                EditView.DataSource = BaseDataProxy.FindQCResults();
        }

        private void btnAppend_Click(object sender, EventArgs e)
        {
            EditQcOptionFrm frm = new EditQcOptionFrm();
            if (TableType == EditTableType.enPromble)
            {
                frm.SetEditAsPromble();
            }
            else
            {
                frm.SetEditAsResult();
            }
            frm.SetEditModeAppend();
            frm.ShowDialog();

            if (frm.CallDone)
            {
                RefreshData();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            EdtSelect();
        }

        private void EdtSelect()
        {
            if (EditView.CurrentRow == null)
                return;

            EditQcOptionFrm frm = new EditQcOptionFrm();
            if (TableType == EditTableType.enPromble)
            {
                frm.SetEditAsPromble();
            }
            else
            {
                frm.SetEditAsResult();
            }
            // 获取需要编辑的记录
            //frm.EditInfo = EditView.CurrentRow.DataBoundItem as RecInfo;
            frm.RecId = (int)EditView.CurrentRow.Cells[1].Value;
            frm.RecDesc = (string)EditView.CurrentRow.Cells[2].Value;
            frm.RecFlag = (bool)EditView.CurrentRow.Cells[3].Value;
            frm.SetEditModeUpdate();
            frm.ShowDialog();

            if (frm.CallDone)
            {
                EditView.CurrentRow.SetValues(false, frm.RecId, frm.RecDesc, frm.RecFlag);
                EditView.Refresh();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (EditView.CurrentRow == null)
                return;
            // Configure message box
            string message = "是否删除记录？";
            string caption = "确认";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            // Show message box
            DialogResult result = MessageBox.Show(message, caption, buttons);
            if (result == DialogResult.Yes)
            {
                try
                {
                    //取绑定到id字段的列名字
                    string sFieldName = "";
                    foreach (DataGridViewColumn cSearch in EditView.Columns)
                    {
                        if (cSearch.DataPropertyName == "id")
                        {
                            sFieldName = cSearch.Name;
                        }
                    }
                    if (EditView.SelectedRows.Count == 0)
                        EditView.CurrentRow.Selected = true;

                    //根据id删除所有选择的纪录
                    foreach (DataGridViewRow rSel in EditView.SelectedRows)
                    {
                        //"idDataGridViewTextBoxColumn"
                        int id = Convert.ToInt16(rSel.Cells[sFieldName].Value);
                        bool CallReturnDone = false;
                        if (TableType == EditTableType.enPromble)
                            CallReturnDone = this.BaseDataProxy.DeleteQCProblemType(id);
                        else
                            CallReturnDone = this.BaseDataProxy.DeleteQCResult(id);

                        if (!CallReturnDone)
                        {
                            RefreshData();
                            MessageBox.Show("无法删除记录.");
                        }
                    }
                    RefreshData();
                    MessageBox.Show("已成功删除记录.");
                }
                catch (InvalidCastException eMsg)
                {
                    MessageBox.Show(eMsg.Message.ToString());
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            GetResult();
        }

        private void GetResult()
        {
            if (!_SearchMode)
                return;

            if (!(EditView.CurrentRow == null))
            {
                SelectItem item = new SelectItem();
                if (TableType == EditTableType.enPromble)
                {
                    item.Key = EditView.CurrentRow.Cells["clId"].Value.ToString();
                    item.Name = EditView.CurrentRow.Cells["clDesc"].Value.ToString();
                    item.Value = EditView.CurrentRow.Cells["clScrapFlag"].Value.ToString();
                }
                else
                {
                    item.Key = EditView.CurrentRow.Cells["clId2"].Value.ToString();
                    item.Name = EditView.CurrentRow.Cells["clDesc2"].Value.ToString();
                    item.Value = EditView.CurrentRow.Cells["clPassFlag"].Value.ToString();
                }
                this.SelectorItem = item;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
